﻿using System;
using System.IO;
using System.Windows;
using System.Windows.Markup;
using System.Windows.Media;
using System.Windows.Controls;

namespace CodeJumper
{
    class ThemeResource
    {
        ResourceDictionary dictionary;
        ApplicationConfiguration AppConfig;
        public ThemeResource(ApplicationConfiguration AppConfig)
        {
            this.AppConfig = AppConfig;
        }

        public bool Load(string Filename)
        {
            var fileName = AppConfig.ExtensionDirectory + @"\Themes\" + Filename;
            
            if (File.Exists(fileName))
            {
                using (FileStream fs = new FileStream(fileName, FileMode.Open))
                {
                    ResourceDictionary dic = (ResourceDictionary)XamlReader.Load(fs);
                    this.dictionary = dic;
                    return true;
                }
            }
            else
            {
                MessageBox.Show(string.Format("Try {0}", Application.ResourceAssembly.Location));
                return false;
            }
        }

        public object FindObject(string key)
        {
            if (dictionary == null)
            {
                MessageBox.Show("No theme loaded.");
                return null;
            }

            if (dictionary.Contains(key) == false)
            {
                MessageBox.Show(string.Format("{0} not found in theme resource.", key));
                return null;
            }

            return dictionary[key];
        }

        public Brush FindBrush(string key)
        {
            return FindObject(key) as Brush;
        }

        public Thickness FindThickness(string key)
        {
            return (Thickness)FindObject(key);
        }

        public Control FindControl(string key)
        {
            return FindObject(key) as Control;
        }


    }
}
